{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算圆周率代码 \n",
    "\n",
    "- 蒙特卡洛方法Mont\n",
    "- 泰勒级数展开\n",
    "- Chudnovsky公式方法\n",
    "- 迭代方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import math\n",
    "\n",
    "def draw(inner_points,outer_points):\n",
    "    import matplotlib.pyplot as plt\n",
    "    fig, ax = plt.subplots(figsize=(5, 5))\n",
    "    inner_x = [x[0] for x in inner_points]\n",
    "    inner_y = [x[1] for x in inner_points]\n",
    "    outer_x = [x[0] for x in outer_points]\n",
    "    outer_y = [x[1] for x in outer_points]\n",
    "    ax.scatter(inner_x, inner_y, color=\"r\",s=1)\n",
    "    ax.scatter(outer_x, outer_y, color=\"g\",s=1)\n",
    "\n",
    "    ax.set_xlim(0, 1)\n",
    "    ax.set_ylim(0, 1)\n",
    "\n",
    "    import numpy as np\n",
    "    theta = np.linspace(0, 2*np.pi, 100)\n",
    "    r = 1\n",
    "    x1 = r*np.cos(theta)\n",
    "    x2 = r*np.sin(theta)\n",
    "    ax.plot(x1, x2,color='black')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def MonPi(number):\n",
    "    N2 = number\n",
    "    N1 = 0.\n",
    "    inner_points = []\n",
    "    outer_points = []\n",
    "    for i in range(N2):\n",
    "        x = random.random()\n",
    "        y = random.random()\n",
    "        if x*x+y*y<=1:\n",
    "            N1+=1\n",
    "            inner_points.append([x,y])\n",
    "        else:\n",
    "            outer_points.append([x,y])\n",
    "    \n",
    "    print(\"Monte PI:\",4*N1/N2)\n",
    "    draw(inner_points,outer_points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def TaylorPi(k):\n",
    "    sum,odd = 0,True\n",
    "    for i in range(1,k):\n",
    "        sum += 1/(2*i-1) if odd==True else -1/(2*i-1)\n",
    "        odd = not odd\n",
    "    print(\"Taylor PI:\",sum*4)\n",
    "    \n",
    "def Ch_cal(k):\n",
    "    uper_value = math.factorial(6*k)*(13591409+545140134*k)\n",
    "    lower_value = math.factorial(3*k)*math.pow(math.factorial(k),3)*math.pow((-640320),3*k)\n",
    "    return uper_value/lower_value\n",
    "\n",
    "def Chudnovsky(number):\n",
    "    uper_value = 426880*math.sqrt(10005)\n",
    "    lower_sum = 0.\n",
    "    for k in range(number):\n",
    "        lower_sum+=Ch_cal(k)\n",
    "    print(\"Chudnovsky PI:\",uper_value/lower_sum)\n",
    "\n",
    "    \n",
    "def Iterative_cal(number):\n",
    "    a_now = 1.\n",
    "    b_now = 1./math.sqrt(2)\n",
    "    t_now = .25\n",
    "    p_now = 1.\n",
    "    for i in range(number):\n",
    "        a = (a_now+b_now)/2\n",
    "        b = math.sqrt(a_now*b_now)\n",
    "        t = t_now-p_now*math.pow((a_now-a),2)\n",
    "        p = 2*p_now\n",
    "\n",
    "        a_now = a\n",
    "        b_now = b\n",
    "        t_now = t\n",
    "        p_now = p\n",
    "    print(\"Iterative PI:\",math.pow(a_now+b_now,2)/(4*t_now))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MonPi(30000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "TaylorPi(1000000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Chudnovsky(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Iterative_cal(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
